/* 
 * @(#)Response.java 2012-5-2
 * Copyright (c) 2012
 * Dark-J (Zh)Co., Ltd.
 * Dark-J R&D,BeiJing
 *
 * All Rights Reserved
 * 
 * Revision History:
 * 								Modification	 Traking
 * Author (Email ID)			Date			 Number				Description
 * ----------------------------------------------------------------------------------
 * yongchong.bai				2012-5-2		 					Initial Version
 */

package com.jworktogether.application2;
 
import java.io.OutputStream; 
import java.io.IOException; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.File; 
import java.io.PrintWriter; import java.util.Locale; 
import javax.servlet.ServletResponse; 
import javax.servlet.ServletOutputStream; 

/**
 * @author baiyc
 * @version 1.0
 */
public class Response implements ServletResponse { 
 
   private static final int BUFFER_SIZE = 1024; 
   Request request; 
   OutputStream output; 
   PrintWriter writer; 
 
   public Response(OutputStream output) { 
     this.output = output; 
   } 
 
    public void setRequest(Request request) { 
     this.request = request; 
   } 
 
   /* This method is used to serve static pages */ 
   public void sendStaticResource() throws IOException { 
     byte[] bytes = new byte[BUFFER_SIZE]; 
     FileInputStream fis = null; 
     try { 
       /* request.getUri has been replaced by request.getRequestURI */ 
       File file = new File(Constants.WEB_ROOT, request.getUri()); 
       fis = new FileInputStream(file); 
       /* 
       HTTP Response = Status-Line 
         *(( general-header | response-header | entity-header ) CRLF) 
         CRLF 
         [ message-body ] 
         Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF 
       */ 
       int ch = fis.read(bytes, 0, BUFFER_SIZE); 
       while (ch!=-1) { 
 
         output.write(bytes, 0, ch); 
         ch = fis.read(bytes, 0, BUFFER_SIZE); 
       } 
     } 
     catch (FileNotFoundException e) { 
       String errorMessage = "HTTP/1.1 404 File Not Found\r\n" + 
         "Content-Type: text/html\r\n" +          "Content-Length: 23\r\n" + 
          "\r\n" + 
         "<h1>File Not Found</h1>"; 
       output.write(errorMessage.getBytes()); 
     } 
     finally { 
       if (fis!=null) 
         fis.close(); 
     } 
   } 
 
 
   /** implementation of ServletResponse  */ 
   public void flushBuffer() throws IOException {} 
   public int getBufferSize() { 
     return 0; 
   } 
   public String getCharacterEncoding() { 
     return null; 
   } 
   public Locale getLocale() { 
     return null; 
   } 
   public ServletOutputStream getOutputStream() throws IOException { 
     return null; 
   } 
    public PrintWriter getWriter() throws IOException { 
     // autoflush is true, println() will flush, 
     // but print() will not. 
     writer = new PrintWriter(output, true); 
     return writer; 
   } 
   public boolean isCommitted() { 
     return false; 
   } 
   public void reset() {  } 
   public void resetBuffer() {  } 
   public void setBufferSize(int size) {  } 
   public void setContentLength(int length) {  } 
   public void setContentType(String type) {  } 
   public void setLocale(Locale locale) {  }

/* (non-Javadoc)
 * @see javax.servlet.ServletResponse#getContentType()
 */
@Override
public String getContentType() {
	// TODO Auto-generated method stub
	return null;
}

/* (non-Javadoc)
 * @see javax.servlet.ServletResponse#setCharacterEncoding(java.lang.String)
 */
@Override
public void setCharacterEncoding(String charset) {
	// TODO Auto-generated method stub
	
} 
}

